package com.mutual.reins.interf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mutual.reins.interf.entity.ReinsPolicyCeded;
import com.mutual.reins.interf.entity.ReinsPolicyInterfDto;
import com.mutual.reins.interf.entity.ReinsPolicyUnit;
import com.mutual.reins.interf.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author panchen
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReinsPolicyInterfServiceImpl implements IReinsPolicyInterfService {
    @Autowired
    private IReinsPolicyUnitService iReinsPolicyUnitService;
    @Autowired
    private IReinsPolicyItemService iReinsPolicyItemService;
    @Autowired
    private IReinsPolicyCededService iReinsPolicyCededService;
    @Autowired
    private IReinsPolicyCoinsService iReinsPolicyCoinsService;
    @Autowired
    private IReinsPolicyEngageService iReinsPolicyEngageService;
    @Autowired
    private IReinsPolicyFeeService iReinsPolicyFeeService;
    @Autowired
    private IReinsPolicyLimitService iReinsPolicyLimitService;
    @Autowired
    private IReinsPolicyPlanService iReinsPolicyPlanService;

    /**
     * 按照某一个字段查询删除接口表数据
     *
     * @param columnName 字段名称
     * @param cerTino    字段值
     */
    @Override
    public void deleteReinsPolicyInterf(String columnName, String cerTino) {
        Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
            put(columnName, cerTino);
        }};
        iReinsPolicyCededService.removeByMap(columnMap);
        iReinsPolicyItemService.removeByMap(columnMap);
        iReinsPolicyCoinsService.removeByMap(columnMap);
        iReinsPolicyEngageService.removeByMap(columnMap);
        iReinsPolicyFeeService.removeByMap(columnMap);
        iReinsPolicyLimitService.removeByMap(columnMap);
        iReinsPolicyPlanService.removeByMap(columnMap);
        iReinsPolicyUnitService.removeByMap(columnMap);
    }

    /**
     * 按照投保单查询接口表数据
     *
     * @param cerTino    字段值
     * @return  ReinsPolicyInterfDto
     */
    @Override
    public ReinsPolicyInterfDto selectReinsPolicyInterf(String cerTino) {
        ReinsPolicyInterfDto reinsPolicyInterfDto = new ReinsPolicyInterfDto();
        Map<String, Object> columnMap = new HashMap<String, Object>(16) {{
            put("PROPOSALNO", cerTino);
        }};
        ReinsPolicyUnit reinsPolicyUnit = iReinsPolicyUnitService.selectReinsPolicyUnit(cerTino);
        reinsPolicyInterfDto.setCerTiNo(reinsPolicyUnit.getProposalNo());
        reinsPolicyInterfDto.setCerTiType(reinsPolicyUnit.getCerTiType());
        reinsPolicyInterfDto.setReinsPolicyUnit(reinsPolicyUnit);
        reinsPolicyInterfDto.setReinsPolicyItem(iReinsPolicyItemService.listByMap(columnMap));
        reinsPolicyInterfDto.setReinsPolicyCeded(iReinsPolicyCededService.listByMap(columnMap));
        reinsPolicyInterfDto.setReinsPolicyCoins(iReinsPolicyCoinsService.listByMap(columnMap));
        reinsPolicyInterfDto.setReinsPolicyEngage(iReinsPolicyEngageService.listByMap(columnMap));
        reinsPolicyInterfDto.setReinsPolicyFee(iReinsPolicyFeeService.listByMap(columnMap));
        reinsPolicyInterfDto.setReinsPolicyLimit(iReinsPolicyLimitService.listByMap(columnMap));
        reinsPolicyInterfDto.setReinsPolicyPlan(iReinsPolicyPlanService.listByMap(columnMap));
        return reinsPolicyInterfDto;
    }

    /**
     * 保存接口表数据
     *
     * @param reinsPolicyInterfDto 接口表数据
     */
    @Override
    public void saveReinsPolicyInterf(ReinsPolicyInterfDto reinsPolicyInterfDto) {
        iReinsPolicyUnitService.save(reinsPolicyInterfDto.getReinsPolicyUnit());
        // 获得标的信息
        if (ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyItem())  && ! reinsPolicyInterfDto.getReinsPolicyItem().isEmpty()) {
            iReinsPolicyItemService.saveBatch(reinsPolicyInterfDto.getReinsPolicyItem());
        }

        // 费用信息
        if ( ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyFee()) && !reinsPolicyInterfDto.getReinsPolicyFee().isEmpty()) {
            iReinsPolicyFeeService.saveBatch(reinsPolicyInterfDto.getReinsPolicyFee());
        }

        // 分期缴费信息
        if (ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyPlan()) && !reinsPolicyInterfDto.getReinsPolicyPlan().isEmpty()) {
            iReinsPolicyPlanService.saveBatch(reinsPolicyInterfDto.getReinsPolicyPlan());
        }

        // 联共保信息
        if (ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyCoins())  && !reinsPolicyInterfDto.getReinsPolicyCoins().isEmpty()) {
            iReinsPolicyCoinsService.saveBatch(reinsPolicyInterfDto.getReinsPolicyCoins());
        }

        // 投保单限额信息
        if (ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyLimit())  && !reinsPolicyInterfDto.getReinsPolicyLimit().isEmpty()) {
            iReinsPolicyLimitService.saveBatch(reinsPolicyInterfDto.getReinsPolicyLimit());
        }

        // 投保单特别约定信息
        if (ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyEngage()) && !reinsPolicyInterfDto.getReinsPolicyEngage().isEmpty()) {
            iReinsPolicyEngageService.saveBatch(reinsPolicyInterfDto.getReinsPolicyEngage());
        }

        // 分入业务信息
        if (ObjectUtil.isNotNull(reinsPolicyInterfDto.getReinsPolicyCeded()) && !reinsPolicyInterfDto.getReinsPolicyCeded().isEmpty()) {
            iReinsPolicyCededService.saveBatch(reinsPolicyInterfDto.getReinsPolicyCeded());
        }

    }

}
